import axios from 'axios'

const prodServer = '';
const devServer = '';

const errorMsg = {
    '0x0000': '成功',
    '0x0001': '服务器内部错误',
}

let useServer = null;

if(process.env.NODE_ENV === 'production') {
	useServer = prodServer;
} else if(process.env.NODE_ENV === 'dev') {
	useServer = devServer;
}

axios.defaults.baseURL = useServer + '/v1';
axios.defaults.timeout = 15000;
// axios.defaults.withCredentials = true;
axios.defaults.headers.common['Login-Token'] = localStorage.getItem('Login-Token');
axios.defaults.headers.post['Content-Type'] = 'application/json';

//添加一个响应拦截器
axios.interceptors.response.use(function(res){
    //在这里对返回的数据进行处理
	res.data.message = errorMsg[res.data.stateCode]
    return res;
},function(err){
    //处理请求错误
    if (err && err.response) {
        switch (err.response.status) {
            case 400:
                err.message = '请求错误'
                break

            case 401:
                err.message = '未授权，请登录'
                location.href='../../views/account/login.html';
                break

            case 403:
                err.message = '拒绝访问'
                break

            case 404:
                err.message = `请求地址出错: ${err.response.config.url}`
                break

            case 408:
                err.message = '请求超时'
                break

            case 500:
                err.message = '服务器内部错误'
                break

            case 501:
                err.message = '服务未实现'
                break

            case 502:
                err.message = '网关错误'
                break

            case 503:
                err.message = '服务不可用'
                break

            case 504:
                err.message = '网关超时'
                break

            case 505:
                err.message = 'HTTP版本不受支持'
                break

            default:
        }
    }

    return Promise.reject(err)
})


export {axios}

